package com.appspot.diasporajava.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.appspot.diasporajava.dao.DiasporaDao;
import com.appspot.diasporajava.dao.PodDao;
import com.appspot.diasporajava.dao.UserDao;
import com.appspot.diasporajava.entities.Pod;
import com.appspot.diasporajava.federation.PublicFeed;
import com.appspot.diasporajava.manager.FederationManager;

@Controller
public class MainController {
	private Log log = LogFactory.getLog(MainController.class);

	@Autowired
	PublicFeed publicFeed;
	
	@Autowired
	PodDao podDao;
	
	@Autowired
	FederationManager federationManager;

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		log.info("Returning hello view");

		return new ModelAndView("hello.jsp");
	}

	@RequestMapping({ "/home" })
	public ModelAndView showHomePage() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("hello");
		return modelAndView;
	}

	@RequestMapping({ "/public" })
	public ModelAndView showPublic() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("public_feed");
		modelAndView
				.addObject(
						"feed",
						publicFeed.getContentList("https://joindiaspora.com/public/u_g_g_l_y.atom"));
		return modelAndView;
	}

	@RequestMapping({ "/users" })
	public ModelAndView users() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("user_list");
		modelAndView.addObject("users", new UserDao().getUsers());
		return modelAndView;
	}

	@RequestMapping({ "/feed/{userId}" })
	public ModelAndView feed(@PathVariable String userId) {

		String feedUrl = new DiasporaDao().getPublicFeed(userId);

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("public_feed");
		modelAndView.addObject("feed", publicFeed.getContentList(feedUrl));
		return modelAndView;
	}
	
	@RequestMapping({"/persistence"})
	public String persistence(){
		
		Pod pod = new Pod();
		pod.setName("joindiaspora");
		pod.setUrl("https://joindiaspora.com");
		pod.setTemplate("https://joindiaspora.com/webfinger?q={uri}");
		
		pod=podDao.create(pod);
		log.debug(pod);
		
		log.debug(podDao.countAll(new HashMap()));
//		ModelAndView modelAndView = new ModelAndView();
//		return modelAndView;
		return "MyView";
	}
	
	@RequestMapping({"/pods"})
	public ModelAndView pods(){
		List<Pod> pods = podDao.list();
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("pods", pods);
		
		
		return modelAndView;
	}
	
	@RequestMapping({ "/adduser/{userEmail}" })
	public ModelAndView addUser(@PathVariable String userEmail) {

		try {
			federationManager.findUser(userEmail);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		ModelAndView modelAndView = new ModelAndView();

		return modelAndView;
	}

}